{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3.函数的参数\n",
    "\n",
    "* <font style=\"color:red\">\n",
    "注意：  \n",
    "    函数的参数可以没有，如果有参数，则如下（分为形式参数和实际参数）：  \n",
    "</font>\n",
    "\n",
    "> 1. 形式参数（形参）  \n",
    ">> 1. 在定义过程中，函数的参数为形式参数；  \n",
    ">> 2. 形式参数是虚拟的，不占用空间  \n",
    "> 2. 实际参数（实参）  \n",
    ">> 1. 调用函数过程中，函数的参数为实际参数 \n",
    ">> 2. 调用函数时，形式参数被分配空间，成为实际参数并应用起来  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "def hello():\n",
    "    \"\"\"打印hello world\"\"\"\n",
    "    print(\"hello world!\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello world!\n"
     ]
    }
   ],
   "source": [
    "# 函数是可以没有参数的\n",
    "hello()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 定义函数\n",
    "def greet_user(username):\n",
    "    \"\"\"显示简单的问候语，输入你的名字\"\"\"\n",
    "    print(\"Hello,\"+username.title()+\"!\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello,Kakio!\n"
     ]
    }
   ],
   "source": [
    "# 调用函数： username = \"Kakio\"\n",
    "greet_user(\"Kakio\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 当参数不止一个时：  \n",
    "> 1. 位置参数（position argument） \n",
    ">> 1. 形式参数有多个值，如果实际参数直接赋值，要按照形式参数的先后顺序，这种方式称：位置参数赋值；  \n",
    ">> 2. 在调用函数的过程中需要考虑：  \n",
    ">>> 1. 考虑位置  \n",
    ">>> 2. 考虑个数  \n",
    "\n",
    "> 2. 关键字参数  \n",
    ">> 1. 在调用函数时使用，直接在实参中用 赋值 的方式定义形参；  \n",
    ">> 2. 不需要考虑顺序，因为赋值已经确定参数的值；  \n",
    "\n",
    "> 3. 默认参数  \n",
    ">> 1. 默认参数写在形式参数中；  \n",
    ">> 2. 默认参数存在的话，实际调用过程中可以省略赋值的过程"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 请注意，有两个参数\n",
    "def describe_pet(animal_type,pet_name):\n",
    "    \"\"\"显示宠物信息\"\"\"\n",
    "    print(\"\\nI have a \"+animal_type+\".\")\n",
    "    print(\"My \"+animal_type+\"'s name is \"+pet_name.title()+\"。\")\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "I have a dog.\n",
      "My dog's name is 钱多多。\n"
     ]
    }
   ],
   "source": [
    "describe_pet(\"dog\",\"钱多多\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "describe_pet() missing 1 required positional argument: 'pet_name'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-21-0d3b24a7fce8>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[1;31m# 位置参数失败案例\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0mdescribe_pet\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"cat\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      3\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      4\u001b[0m \u001b[1;31m# TypeError: describe_pet() missing 1 required positional argument: 'pet_name'\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mTypeError\u001b[0m: describe_pet() missing 1 required positional argument: 'pet_name'"
     ]
    }
   ],
   "source": [
    "# 位置参数失败案例\n",
    "describe_pet(\"cat\")\n",
    "\n",
    "# TypeError: describe_pet() missing 1 required positional argument: 'pet_name'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "I have a dog.\n",
      "My dog's name is 钱多多。\n"
     ]
    }
   ],
   "source": [
    "# 关键字参数\n",
    "describe_pet(animal_type=\"dog\",pet_name=\"钱多多\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 默认参数（写在形式参数中）\n",
    "\n",
    "def describe_pet(pet_name,animal_type=\"dog\"):\n",
    "    \"\"\"显示宠物信息,animal_type 的默认值是'dog'\"\"\"\n",
    "    print(\"\\nI have a \"+animal_type+\".\")\n",
    "    print(\"My \"+animal_type+\"'s name is \"+pet_name.title()+\"。\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "I have a dog.\n",
      "My dog's name is 钱多多。\n"
     ]
    }
   ],
   "source": [
    "describe_pet(\"钱多多\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
